package com.lxr.test5002;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.lxr.test5002.base.BaseActivity;

import butterknife.BindView;

/**
 * 类描述:[闪光灯测试]
 * 说明:
 * @author: LXR-zsm
 * @Date: 2023/12/18 11:17
 */
public class FlashActivity extends BaseActivity {
    @BindView(R.id.iv_flash_drawable)
    ImageView iv_flash_drawable;
    @BindView(R.id.btn_flash_switch)
    Button btn_flash_switch;

    private CameraManager mCameraManager;
    private boolean mTorchAvailable;
    private String mCameraId;
    private boolean mFlashlightEnabled;
    private int flashLightPowerstatus;

    public FlashActivity() {
        super(R.layout.activity_flash);
    }


    @Override
    protected void initView(Bundle savedInstanceState) {

        mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
        try {
            mCameraId = getCameraId();
        } catch (Throwable e) {
            Log.e("camera", "Couldn't initialize.", e);
            return;
        }
        // 注册针对闪光灯的状态回调
        if (mCameraId != null) {
            mCameraManager.registerTorchCallback(mTorchCallback, null);
        }

        if (!hasFlashlight()) {
            Toast.makeText(this, "不支持闪光灯", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void initEvent() {
        btn_flash_switch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flashLightPowerstatus == 0) {
                    flashLightPowerstatus = 1;
                    iv_flash_drawable.setBackgroundResource(R.drawable.flash_on);
                    setFlashlight(true);

                } else {
                    flashLightPowerstatus = 0;
                    iv_flash_drawable.setBackgroundResource(R.drawable.flash_off);
                    setFlashlight(false);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }


    // 判断是否支持
    public boolean hasFlashlight() {
        return this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    private String getCameraId() throws CameraAccessException {
        String[] ids = mCameraManager.getCameraIdList();
        for (String id : ids) {
            CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
            Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
            Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
            if (flashAvailable != null && flashAvailable
                    && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
                return id;
            }
        }
        return null;
    }

    // 状态回调
    private final CameraManager.TorchCallback mTorchCallback =
            new CameraManager.TorchCallback() {

                @Override
                public void onTorchModeUnavailable(String cameraId) {
                    if (TextUtils.equals(cameraId, mCameraId)) {
                        setCameraAvailable(false);
                    }
                }

                @Override
                public void onTorchModeChanged(String cameraId, boolean enabled) {
                    if (TextUtils.equals(cameraId, mCameraId)) {
                        setCameraAvailable(true);
                        mFlashlightEnabled = enabled;
                    }
                }

                private void setCameraAvailable(boolean available) {
                    mTorchAvailable = available;
                }
            };

    // 开启/关闭闪光灯
    public void setFlashlight(boolean enabled) {
        synchronized (this) {
            if (mCameraId == null || !mTorchAvailable) {
                return;
            }
            if (mFlashlightEnabled != enabled) {
                mFlashlightEnabled = enabled;
                try {
                    mCameraManager.setTorchMode(mCameraId, enabled);
                } catch (CameraAccessException e) {
                    Log.e("camera", "Couldn't set torch mode", e);
                }
            }
        }
    }
}

